package com.lanou.myapp.comic.themecomic.contentthemecomic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.GsonRequest;
import com.lanou.myapp.imageloader.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/18.
 */
public class ContentThemeComicActivity extends AppCompatActivity {
    private ListView listView;
    private ContentThemeComicAdapter adapter;
    private String url;
    private TextView tv_title, tv_description;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_theme_comic);
        listView = (ListView) findViewById(R.id.listView_content_theme_comic);

        View headView = LayoutInflater.from(this).inflate(R.layout.head_theme_comic, null);
        tv_title = (TextView) headView.findViewById(R.id.tv_head_theme_comicss);
        tv_description = (TextView) headView.findViewById(R.id.tv_head_description_theme_comicss);
        imageView = (ImageView) headView.findViewById(R.id.iv_head_theme_comic);


        listView.addHeaderView(headView);

        Intent intent = getIntent();
        Integer a = intent.getIntExtra("key", 0);
        String b = "http://v2.api.dmzj.com/subject/";
        String c = ".json";
        url = b + a + c;
//        Log.d("sssfdf", url);

        adapter = new ContentThemeComicAdapter(ContentThemeComicActivity.this);

        GsonRequest<ContentThemeComicBeans> request = new GsonRequest<ContentThemeComicBeans>(url, ContentThemeComicBeans.class, new Response.Listener<ContentThemeComicBeans>() {
            @Override
            public void onResponse(ContentThemeComicBeans response) {
                adapter.addData(response.getComics());
                listView.setAdapter(adapter);
                tv_title.setText(response.getTitle());
                tv_description.setText(response.getDescription());
                ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,R.mipmap.v6_boy_unseletor_icon,R.mipmap.v6_boy_unseletor_icon );
                VolleySingleton.getImageLoader().get(response.getMobile_header_pic(),listener,400,300);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        VolleySingleton.getInstance()._addRequest(request);

//        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                Gson gson = new Gson();
//                data = new ArrayList<>();
//                ContentThemeComicBeans beans = gson.fromJson(response, ContentThemeComicBeans.class);
//                data = beans.getComics();
//                datas = new ContentThemeComicBeans();
////                Log.d("weqwewqe",data.size() + "");
//                adapter.addData(data);
//
//                tv_title.setText(beans.getTitle());
//                tv_description.setText(beans.getDescription());
//                listView.setAdapter(adapter);
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                error.printStackTrace();
//            }
//        });
//        queue.add(request);

    }
}
